package py.edu.ucom.cadira.net.serializables;

import org.jdom.Document;
import org.jdom.Element;

import py.edu.ucom.cadira.net.User;
import py.edu.ucom.cadira.net.XMLSerializable;

public class LoginResponse implements XMLSerializable {
	private User usuario;
	private boolean status;
    private static final String NODE_LOGIN = "login";

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public LoginResponse(){
	this.usuario = new User();	
	}

	public LoginResponse(boolean status) {
		super();
		setStatus(status);
		this.usuario = new User();
	}
	
	public User getUsuario() {
		return usuario;
	}
	public void setUsuario(User usuario) {
		this.usuario = usuario;
	}

	public void fromXML(Document xml) {
		
		Element root = xml.getRootElement();
		Element loginNode = root.getChild(NODE_LOGIN);
		Element statusNode = loginNode.getChild("status");
		setStatus(Boolean.parseBoolean(statusNode.getValue()));
		Element userNode = loginNode.getChild("usuario");
		setUsuario(getUsuario().fromXml(userNode));
		
	}

	public void toXML(Element root) {

		Element loginNode = new Element(NODE_LOGIN);
		Element statusNode = new Element("status");
		statusNode.setText(String.valueOf(isStatus()));
		Element userNode = this.usuario.toXml();
		loginNode.addContent(statusNode);
		loginNode.addContent(userNode);
		root.addContent(loginNode);
		
	}
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof LoginResponse)) {
			return false;
		}
		LoginResponse otherObj = (LoginResponse) obj;
		
		if(otherObj.isStatus()==this.isStatus()) {
			return true;
		}
		return false;
	}	
}
